class ChatModel {
  final int id;
  final int chatId;
  final int fromUserId;
  final int toUserId;
  final String? content;
  final int messageType; // 1-文本，2-图片，3-文件等
  final int status; // 0-发送中，1-已发送，2-已读
  final DateTime? createdAt;

  ChatModel({
    required this.id,
    required this.chatId,
    required this.fromUserId,
    required this.toUserId,
    this.content,
    required this.messageType,
    required this.status,
    this.createdAt,
  });

  factory ChatModel.fromJson(Map<String, dynamic> json) {
    return ChatModel(
      id: json['id'] ?? 0,
      chatId: json['chat_id'] ?? json['chatId'] ?? 0,
      fromUserId: json['from_user_id'] ?? json['fromUserId'] ?? 0,
      toUserId: json['to_user_id'] ?? json['toUserId'] ?? 0,
      content: json['content'],
      messageType: json['message_type'] ?? json['messageType'] ?? 1,
      status: json['status'] ?? 1,
      createdAt: json['created_at'] != null
          ? DateTime.parse(json['created_at'])
          : json['createdAt'] != null
              ? DateTime.parse(json['createdAt'])
              : null,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'chat_id': chatId,
      'from_user_id': fromUserId,
      'to_user_id': toUserId,
      'content': content,
      'message_type': messageType,
      'status': status,
      'created_at': createdAt?.toIso8601String(),
    };
  }
}

/// 最近聊天项（用于首页展示）
class RecentChatModel {
  final int userId;
  final String username;
  final String? nickname;
  final String? avatar;
  final String? lastMessage;
  final DateTime? lastMessageTime;
  final int unreadCount;

  RecentChatModel({
    required this.userId,
    required this.username,
    this.nickname,
    this.avatar,
    this.lastMessage,
    this.lastMessageTime,
    this.unreadCount = 0,
  });

  String get displayName => nickname ?? username;
}

